import { HttpClient, HttpEventType, HttpRequest } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzUploadFile } from 'ng-zorro-antd/upload';
import { res } from 'src/common/url.service';

@Component({
  selector: 'app-bgd',
  templateUrl: './bgd.component.html',
  styleUrls: ['../shpCommon.less', './bgd.component.less'],
})
export class BGDComponent implements OnInit {
  constructor(private http: HttpClient, private message: NzMessageService) {}

  ngOnInit(): void {}
  current = 0;
  accept = '.shp,.cpg,.dbf,.prj,.qmd,.shx';

  shpFileList = [];
  pdfFileList: NzUploadFile[] = [];
  csvFileList: NzUploadFile[] = [];

  beforeUploadpdf = (file: NzUploadFile): boolean => {
    this.pdfFileList = this.pdfFileList.concat(file);
    return false;
  };

  beforeUploadcsv = (file: NzUploadFile): boolean => {
    if (file.name == 'DESIGNATED_POINT.csv') {
      this.csvFileList = this.csvFileList.concat(file);
    } else {
      this.message.error('请上传正确的csv文件!');
    }
    return false;
  };
  beforeUploadshp = (file: NzUploadFile): boolean => {
    this.shpFileList = this.shpFileList.concat(file);
    return false;
  };

  next1() {
    if (this.shpFileList.length <= 0) {
      this.message.error('请选择shp文件!');
      return;
    }
    this.current = 1;
  }
  next2() {
    if (this.csvFileList.length <= 0) {
      this.message.error('请上传csv文件!');
      return;
    }
    this.current = 2;
  }

  next3() {
    if (this.pdfFileList.length <= 0) {
      this.message.error('请上传pdf文件!');
      return;
    }

    const files = [
      ...this.shpFileList,
      ...this.pdfFileList,
      ...this.csvFileList,
    ];

    const formData = new FormData();

    files.forEach((file: any) => {
      formData.append('files[]', file);
    });

    this.http
      .post(`http://127.0.0.1:7001/shp/bgd/create`, formData)
      .subscribe((res: res) => {
        if (res.code == 200) this.current = 3;
        else {
          this.message.error(res.msg);
        }
      });
  }

  dwonloadPercent = 0;

  download(evt: any) {
    evt.preventDefault();

    this.http
      .get(`http://127.0.0.1:7001/shp/bgd/package`)
      .subscribe((res: res) => {
        if (res.code == 200) {
          this.http
            .request(
              new HttpRequest('GET', res.data, {
                reportProgress: true,
                responseType: 'blob',
              })
            )
            .subscribe((res) => {
              switch (res.type) {
                case HttpEventType.DownloadProgress: {
                  this.dwonloadPercent = (res.loaded / res.total) * 100;
                  break;
                }
                case HttpEventType.Response: {
                  const url = URL.createObjectURL(res.body);
                  const a = document.createElement('a');
                  a.href = url;
                  a.download = '报告点.zip';
                  a.click();
                  this.dwonloadPercent = 0;
                }
              }
            });
        } else {
          this.message.error(res.msg);
        }
      });
  }
}
